package edu.wtbu.cs.androiddemo;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import edu.wtbu.cs.androiddemo.activity.EventListenSampleActivity;
import edu.wtbu.cs.androiddemo.activity.FullscreenActivity;
import edu.wtbu.cs.androiddemo.activity.HttpUrlConSampleActivity;
import edu.wtbu.cs.androiddemo.activity.ListComponentSampleActivity;
import edu.wtbu.cs.androiddemo.activity.LoginActivity;
import edu.wtbu.cs.androiddemo.activity.PageJumpAndSwitchSampleActivity;
import edu.wtbu.cs.androiddemo.extra.FaceDetectV2Activity;
import edu.wtbu.cs.androiddemo.extra.FaceDetectionActivity;
import edu.wtbu.cs.androiddemo.extra.VideoSampleActivity;
import edu.wtbu.cs.androiddemo.activity.store.FileStoreSampleActivity;
import edu.wtbu.cs.androiddemo.activity.store.SDStoreSampleActivity;
import edu.wtbu.cs.androiddemo.activity.SQLiteDemoActivity;
import edu.wtbu.cs.androiddemo.extra.VideoSnapSampleActivity;
import edu.wtbu.cs.androiddemo.layout.FrameLayoutDemoActivity;
import edu.wtbu.cs.androiddemo.layout.GridLayoutDemoActivity;
import edu.wtbu.cs.androiddemo.layout.LinearLayoutDemoActivity;
import edu.wtbu.cs.androiddemo.layout.RelativeLayoutDemoActivity;
import edu.wtbu.cs.androiddemo.layout.TableLayoutDemoActivity;

import edu.wtbu.cs.androiddemo.activity.ButtonImageActivity;

public class MainActivity extends AppCompatActivity {

    public static String logTag = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    public void onClickFaceDetectButton(View view){
        Log.i(logTag,"onClickFaceDetectButton");
        Intent intent = new Intent(MainActivity.this, FaceDetectV2Activity.class);
        startActivity(intent);
    }
    public void  clickFullScreenButton(View view){
        Log.i(logTag,"clickFullScreenButton");
        Intent intent = new Intent(MainActivity.this, FullscreenActivity.class);
        startActivity(intent);
    }

    public void  clickVideoSnapPlayButton(View view){
        Log.i(logTag,"clickVideoSnapPlayButton");
        Intent intent = new Intent(MainActivity.this, VideoSnapSampleActivity.class);
        startActivity(intent);
    }
    public void  clickVideoPlayButton(View view){
        Log.i(logTag,"clickVideoPlayButton");
        Intent intent = new Intent(MainActivity.this, VideoSampleActivity.class);
        startActivity(intent);
    }

    //Retrofit
    public void  clickRetrofitButton(View view){
        Log.i(logTag,"clickRetrofitButton");
        Intent intent = new Intent(MainActivity.this, HttpUrlConSampleActivity.class);
        intent.putExtra("userOkHttp","2");
        startActivity(intent);
    }

    //OkHttp
    public void  clickOkHttpButton(View view){
        Log.i(logTag,"clickOkHttpButton");
        Intent intent = new Intent(MainActivity.this, HttpUrlConSampleActivity.class);
        intent.putExtra("userOkHttp","1");
        startActivity(intent);
    }


    //HttpURLConnection
    public void  clickHttpURLConnection(View view){
        Log.i(logTag,"clickAutoLoginButton");
        Intent intent = new Intent(MainActivity.this, HttpUrlConSampleActivity.class);
        intent.putExtra("userOkHttp","0");
        startActivity(intent);
    }
    //SQLiteSample
    public void clickSQLiteSampleButton(View view){
        Log.i(logTag,"clickAutoLoginButton");
        Intent intent = new Intent(MainActivity.this, SQLiteDemoActivity.class);
        startActivity(intent);
    }

    //自动登录
    public void clickAutoLoginButton(View view){
        Log.i(logTag,"clickAutoLoginButton");
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    //SD存储
    public void clickSdStoreSampleButton(View view){
        Log.i(logTag,"clickSdStoreSampleButton");
        Intent intent = new Intent(MainActivity.this, SDStoreSampleActivity.class);
        startActivity(intent);
    }


    /**
     * 文件存储Demo
     *
     * @param view
     */
    public void clickFileStoreSampleButton(View view){
        Log.i(logTag,"clickFileStoreSampleButton");
        Intent intent = new Intent(MainActivity.this, FileStoreSampleActivity.class);
        startActivity(intent);
    }

    public void clickEventListenSampleButton(View view){
        Log.i(logTag,"clickEventListenSampleButton");
        Intent intent = new Intent(MainActivity.this, EventListenSampleActivity.class);
        startActivity(intent);
    }
    public void clickListComponentSampleButton(View view){
        Log.i(logTag,"clickListComponentSampleButton");
        Intent intent = new Intent(MainActivity.this, ListComponentSampleActivity.class);
        startActivity(intent);
    }

    public void clickPayDialog(View view){
        AlertDialog.Builder paybuilder = new AlertDialog.Builder(MainActivity.this);
        View payView = getLayoutInflater().inflate(R.layout.paydialog,null);

        //需要在支付对话框里面找，而不是findview 或者 view。findview
        TextView text = payView.findViewById(R.id.sum_money);
        text.setText("需付款？100元");
        paybuilder.setView(payView);
        paybuilder.setPositiveButton("确 认 支 付", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AlertDialog.Builder sucbuilder = new AlertDialog.Builder(MainActivity.this);
                sucbuilder.setTitle("恭喜您支付成功");
                sucbuilder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                sucbuilder.create().show();
            }
        });
        paybuilder.setNegativeButton("取消付款", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        paybuilder.create().show();
    }

    public void clickProcessDialogWithNumber(View view){
        Log.i(logTag, "clickProcessDialogWithNumber");
        ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setTitle("带进度条的对话框");
        dialog.setMessage("加载中~");
        dialog.setMax(200);
        dialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<=200;i++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    dialog.setProgress(i);
                }
            }
        }).start();

    }
    public void clickProcessDialog(View view){
        Log.i(logTag, "clickProcessDialog");
        ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        dialog.setTitle("不带进度条的对话框");
        dialog.setMessage("正在加载中~");
        dialog.show();
    }

    public void clickListDialogMultiChoice(View view){
        Log.i(logTag, "clickListDialogMultiChoice");
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("多选对话框");
        final String[] items = {"数学","语文","英语","政治"};
        final boolean[] checkItems = {false,true,false,false};
        builder.setMultiChoiceItems(items, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked){
                    checkItems[which]=true;
                }else {
                    checkItems[which]=false;
                    Toast.makeText(MainActivity.this,"您取消了"+items[which],Toast.LENGTH_LONG).show();
                }
            }
        });


        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String checkItem = "";
                for (int i=0;i< checkItems.length;i++){
                    if ( checkItems[i]==true)
                        checkItem=checkItem+items[i];
                }

                Toast.makeText(MainActivity.this,"您选择了："+checkItem,Toast.LENGTH_LONG).show();
            }
        });
        builder.create().show();

    }

    /**
     * 单选对话框
     * @param view
     */
    public void clickListDialogSingleChoice(View view){
        Log.i(logTag, "clickListDialogSingleChoice");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("单选对话框");
        final String[] items = {"男","女"};
        final boolean[] checkItems = {true,false};
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for (int i=0;i< checkItems.length;i++){
                    checkItems[i]=false;
                }
                checkItems[which]=true;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(MainActivity.this,"您单击了取消按钮",Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String checkItem = "";
                for (int i=0;i< checkItems.length;i++){
                    if ( checkItems[i]==true)
                        checkItem=items[i];
                }

                Toast.makeText(MainActivity.this,"您选择了："+checkItem,Toast.LENGTH_LONG).show();
            }
        });
        builder.create().show();
    }

    /**
     * 点击普通对话框
     * @param view
     */
    public void clickAlertDialogButton(View view){
        Log.i(logTag, "clickAlertDialogButton");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("普通对话框");
        builder.setMessage("恭喜您支持成功");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"您单击了取消按钮",Toast.LENGTH_LONG).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"您单击了确定按钮",Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }


    public void clickJumpAndSwitchSampleButton(View view){
        Log.i(logTag, "clickJumpAndSwitchSampleButton");
        Intent intent = new Intent(MainActivity.this, PageJumpAndSwitchSampleActivity.class);
        startActivity(intent);
    }
    /**
     * 参考@code clickShapeButton
     */
    public void clickSelectButton() {

    }

    /*
     * Android Studio 4.1之后的版本进行开发时，创建的项目默认的主题所有Button都是Material类型的Button，
     * button通过backgroud修改背景颜色失效;
     * <Button
        android:id="@+id/button"
    改为-------->
       <android.widget.Button
           android:id="@+id/button4"
     * 默认使用主题色，所以想要修改颜色，就要把默认主题给关了或替代了。
     */
    public void clickShapeButton(View view) {
        Log.i(logTag, "clickShapeButton");
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    public void clickImageButton(View view) {
        Log.i(logTag, "clickImageButton");
        Intent intent = new Intent(MainActivity.this, ButtonImageActivity.class);
        startActivity(intent);
    }

    public void clickLinearLayoutDemoButton(View view) {
        Log.i(logTag, "clickLinearLayoutDemoButton");
        Intent intent = new Intent(MainActivity.this, LinearLayoutDemoActivity.class);
        startActivity(intent);
    }

    public void clickTableLayoutDemoButton(View view) {
        Log.i(logTag, "clickTableLayoutDemoButton");
        Intent intent = new Intent(MainActivity.this, TableLayoutDemoActivity.class);
        startActivity(intent);
    }

    public void clickRelativeLayoutDemoButton(View view) {
        Log.i(logTag, "clickRelativeLayoutDemoButton");
        Intent intent = new Intent(MainActivity.this, RelativeLayoutDemoActivity.class);
        startActivity(intent);
    }

    public void clickFrameLayoutDemoButton(View view) {
        Log.i(logTag, "clickFrameLayoutDemoButton");
        Intent intent = new Intent(MainActivity.this, FrameLayoutDemoActivity.class);
        startActivity(intent);
    }

    public void clickGridLayoutDemoButton(View view) {
        Log.i(logTag, "clickGridLayoutDemoButton");
        Intent intent = new Intent(MainActivity.this, GridLayoutDemoActivity.class);
        startActivity(intent);
    }
}